<template>
  <div class="uni-tabs-content-croll">
    <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '110px'" class="uni-scroll">
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item label="唯一登录" prop="oneOnline">
            <el-switch v-model="dataForm.oneOnline"></el-switch>
            <br />
            <span class="form-item-explain">开启后同一个账号只能登录一次，重复登录会被替换下线</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item label="初始密码" prop="initPassword">
            <el-input v-model="dataForm.initPassword" clearable></el-input>
            <br />
            <span class="form-item-explain"> 用户默认密码，密码重置时使用此密码。</span>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item label="短信验证码模板" prop="smsTemplateId">
            <el-input v-model="dataForm.smsTemplateId" placeholder="短信验证码模板" clearable></el-input>
            <br />
            <span class="form-item-explain">登录、注册时默认发送短信验证码模板</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item label="邮件验证码模板" prop="emailTemplateId">
            <el-input v-model="dataForm.emailTemplateId" placeholder="邮件验证码模板" clearable></el-input>
            <br />
            <span class="form-item-explain">登录、注册时默认发送邮件验证码模板</span>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item label="邮件验证码标题" prop="emailTitle">
            <el-input v-model="dataForm.emailTitle" placeholder="邮件验证码标题" clearable></el-input>
            <br />
            <span class="form-item-explain">登录、注册时默认发送邮件标题</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item label="验证码长度" prop="codeLength">
            <el-input v-model.number="dataForm.codeLength" placeholder="验证码长度" clearable></el-input>
            <br />
            <span class="form-item-explain">验证码长度</span>
          </el-form-item>
        </el-col>

      </el-row>
      <el-row>
        <el-col :lg="12" :md="14" :sm="16" :xs="24">
          <el-form-item label="超时时间" prop="expiresSeconds">
            <el-input v-model.number="dataForm.expiresSeconds" placeholder="超时时间" clearable></el-input>
            <br />
            <span class="form-item-explain">验证码超时时间</span>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-col :span="24" style="text-align:center">
        <el-button size="small" type="primary" @click="dataFormSubmitHandle()">{{ $t('save') }}</el-button>
      </el-col> -->
    </el-form>
    <div class="uni-tab-form-bottom">
      <el-button size="small" type="primary" @click="dataFormSubmitHandle()">{{ $t('save') }}</el-button>
    </div>
  </div>
</template>

<script>
import debounce from 'lodash/debounce'
// import mainContent from '@/views/main-content'
// var content = mainContent
export default {
  data () {
    return {
      timestamp: this.$route.params.timestamp,
      loading: false,
      dataForm: {
        dictLoad: true,
        oneOnline: false,
        initPassword: '',
        smsTemplateId: '',
        emailTemplateId: '',
        emailTitle: '',
        codeLength: 6,
        expiresSeconds: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        dictLoad: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        oneOnline: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        initPassword: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        codeLength: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        expiresSeconds: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        smsTemplateId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        emailTemplateId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        emailTitle: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getInfo()
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/sys/appConfig/sys`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http['post']('/sys/appConfig/sys', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style scoped>
</style>
